package com.sgb.kjwl.viewmodel.home;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.BossEntity;
import com.sgb.kjwl.model.entity.DemandHallListEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.material.CommodityCenterFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者:张磊
 * 日期:2021/1/7 0007
 * 说明:1.3.6 商品中间的UI 懂得都懂
 */
@Deprecated
public class CommodityCenterModel extends BaseViewModel {

    private final CommodityCenterFragment mCommodityCenterFragment;
    private final HomeRepertory mHomeRepertory;

    public CommodityCenterModel(CommodityCenterFragment fragment, HomeRepertory repertory) {
        mCommodityCenterFragment = fragment;
        mHomeRepertory = repertory;
    }

    //获取经纬度信息
    public void getLocationLL() {
        DialogHelper.showProgressDialog(mCommodityCenterFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getLocationLL(mHomeRepertory.getAreaCode(), "", "", "").enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mCommodityCenterFragment.getLocationLL(response.body().getData());
                } else {
                    MToast.showToast(mCommodityCenterFragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mCommodityCenterFragment.getActivity(), t.getMessage());
            }
        });
    }


    //首页推荐供应商
    public void getRecommandSupplier(double longitude, double latitude) {
        Map map = new HashMap();
        map.put("longitude", latitude);
        map.put("latitude", longitude);
        map.put("areaCode", mHomeRepertory.getAreaCode());
        APIManager.getInstance().getCommonAPI().getRecommandSupplier2(map).enqueue(new ICallBack<BaseEntity<List<BossEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<BossEntity>> data) {
                if (data.getData() == null) return;
                mCommodityCenterFragment.showOptionView(data.getData());
            }
        });
    }


    // 获取商品首页需求的5个item数据
    public void getDemandHallList() {
        Map map = new HashMap();
        map.put("limit", 5);
        map.put("page", 1);
        map.put("sortType", 2);
        DialogHelper.showProgressDialog(mCommodityCenterFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getDemandHallList(map).enqueue(new Callback<BaseEntity<ListResponse<List<DemandHallListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Response<BaseEntity<ListResponse<List<DemandHallListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    mCommodityCenterFragment.setDemandHallList(response.body().getData().getList());
                } else {
                    MToast.showToast(mCommodityCenterFragment.getContext(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mCommodityCenterFragment.getContext(), t.getMessage());
            }
        });
    }

    //需求列表详情界面
    public void showUrl(String no) {
        DialogHelper.showProgressDialog(mCommodityCenterFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLookingforAJobAPI().getUrlSetting("enquiryDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!TextUtils.isEmpty(response.body().getData().getUrl())) {
                        String url = response.body().getData().getUrl() + "?no=" + no;
                        WebViewX5HostActivity.start(mCommodityCenterFragment.getActivity(), url);
                        //存储商品跳转h5标记
                        SharedPreferenceUtil.getInstance().saveString("EVENT_SHOPPING_TYPE", "event_shopping");
                    } else {
                        WebViewX5HostActivity.start(mCommodityCenterFragment.getActivity(), "");
                    }
                } else {
                    MToast.showToast(mCommodityCenterFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mCommodityCenterFragment.getContext(), t.getMessage());
            }
        });
    }
}